﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.DataProvider;
using CuaHangMayTinh.Utilities;
using System.Data;

namespace CuaHangMayTinh.DataAccessLayer
{
    public class PhanQuyenDA
    {
        public PhanQuyenDA()
        {

        }

        /// <summary>
        /// Hàm thêm 1 Phân Quyền mới
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(PhanQuyenEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("IDQ", entity.IDQ);
                pb.AddParameter("IDSGU", entity.IDSGU);
                pb.AddParameter("Diem", entity.Diem);

                return (int)DBFactory.Database.ExecuteNonQuery("PhanQuyen_Insert", pb.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm Cập nhật 1 phân Quyền
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpDate(PhanQuyenEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("IDPQ", entity.IDPQ);
                pb.AddParameter("IDQ", entity.IDQ);
                pb.AddParameter("IDSGU", entity.IDSGU);
                pb.AddParameter("Diem", entity.Diem);

                int numRowEffected = DBFactory.Database.ExecuteNonQuery("PhanQuyen_Update", pb.Parameters);
                return (numRowEffected > 0);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm tìm tất cả thông tin phân QUyền
        /// </summary>
        /// <returns></returns>
        public DataTable Find_All()
        {
            try
            {
                return DBFactory.Database.FillDataTable("SELECT * FROM PhanQuyen");
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm tìm phân quyền bằng IDSubGroupUser và  IDQuyen
        /// </summary>
        /// <param name="IDSubGroupUser"></param>
        /// <param name="Quyen"></param>
        /// <returns></returns>
        public DataTable Find_SubGroupUser_Quyen(int IDSubGroupUser, int Quyen)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("IDSubGroupUser", IDSubGroupUser);
                pd.AddParameter("Quyen", Quyen);

                dt = DBFactory.Database.FillDataTable("PhanQuyen_Find_SubGroupUser_Quyen", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm UpdateDiem
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update_Diem(PhanQuyenEntity entity)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("IDSGU", entity.IDSGU);
                pd.AddParameter("IDQ", entity.IDQ);
                pd.AddParameter("Diem", entity.Diem);

                return  DBFactory.Database.ExecuteNonQuery("PhanQuyen_Update_Diem", pd.Parameters) > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm tìm phân quyền bằng ID Quyền và IDTaiKhoan
        /// </summary>
        /// <param name="IDSubGroupUser"></param>
        /// <param name="Quyen"></param>
        /// <returns></returns>
        public DataTable Find_TaiKhoan_Quyen(int IDQuyen, int IDTaiKhoan)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("IDQuyen", IDQuyen);
                pd.AddParameter("IDTaiKhoan", IDTaiKhoan);

                dt = DBFactory.Database.FillDataTable("PhanQuyen_Find_TaiKhoan_Quyen", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm tìm Phân Quyền bằng IDQuyen và ID Nhân Viên
        /// </summary>
        /// <param name="IDQuyen"></param>
        /// <param name="IDNhanVien"></param>
        /// <returns></returns>
        public DataTable Find_NhanVien_Quyen(int IDQuyen, int IDNhanVien)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("IDQuyen", IDQuyen);
                pd.AddParameter("IDNhanVien", IDNhanVien);

                dt = DBFactory.Database.FillDataTable("PhanQuyen_Find_NhanVien_Quyen", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm xóa 1 phân quyền
        /// </summary>
        /// <param name="IDQ"></param>
        /// <param name="IDSGU"></param>
        /// <returns></returns>
        public bool Delete(int IDQ, int IDSGU)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("IDQ", IDQ);
                pd.AddParameter("IDSGU", IDSGU);

                return  DBFactory.Database.ExecuteNonQuery("PhanQuyen_Delete", pd.Parameters) > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm chuyển 1 datatble thành 1 List PhanQuyenEntity
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<PhanQuyenEntity> ConvertToList(DataTable dt)
        {
            List<PhanQuyenEntity> list = new List<PhanQuyenEntity>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int idpq = (int)dt.Rows[i][0];
                int idq = (int)dt.Rows[i][1];
                int idsgu = (int)dt.Rows[i][2];
                int diem = (int)dt.Rows[i][3];

                PhanQuyenEntity a = new PhanQuyenEntity(idpq, idq, idsgu, diem);
                list.Add(a);
            }
            return list;
        }
    }
}
